<?php
class Template
{
	var $_nombreBody;
	var $_nombreCabecera;
	var $_estilos;
	var $_scriptCabecera;
	var $_javaScript;
	var $_menu;
	var $_formulario;
	var $_parse;
	function set_NombreBody ( $nombreBody )
	{
	$this->_nombreBody=$nombreBody;
	}
	function set_NombreCabecera($nombreCabecera)
	{
	$this->_nombreCabecera=$nombreCabecera;
	}

	function set_Style($estilos)
	{
	$this->_estilos= $estilos;
	}

	function set_ScriptCabecera($scriptCabecera)
	{
	$this->_scriptCabecera= $scriptCabecera;
	}

	function set_JavaScript($javaScript)
	{
	$this->_javaScript=$javaScript;
	}
	function TincludeLang($filename,$ext='.mo')
	{
	global $lp_root_path,$lang;

	include($lp_root_path."language/".$lang['LANGUAGE'].'/'.$filename.$ext);
	}
	function Tgettemplate($templatename)
	{ 
		global $lp_root_path;
		$filename =  $lp_root_path .'templates/' . $templatename . ".tpl";
		return ReadFromFile($filename);
	}

	function mostrar()
	{
	$this->_page = $this->Tparsetemplate($this->Tgettemplate($this->_nombreBody),$this->_parse);
	$this->_parse['Estilos']=$this->_estilos;
	$this->_parse['JavaScript']=$this->_javaScript;
	$this->_parse['ScriptCabecera']=$this->_scriptCabecera;
	$this->_parse['Formulario']=$this->_page;
	echo $this->Tparsetemplate($this->Tgettemplate($this->_nombreCabecera),$this->_parse);


	}

	function Tparsetemplate($template, $array)
	{

		foreach($array as $a => $b) {
			$template = str_replace("{{$a}}", $b, $template);
		}
		return $template;
	}	
	function generaMenu()
	{
	global $lang,$lp_root_path,$phpEx;
	$this->TincludeLang('menu');
	
	if($_SESSION['usuario']=='admin'){

			$this->_parse['Menu']='<ul id="menu">';
			$this->_parse['Menu'].=$this->Tparsetemplate($this->Tgettemplate('menu_admin'), $lang);
			$this->_parse['Menu'].=$this->Tparsetemplate($this->Tgettemplate('menu_jugador'), $lang);
			$this->_parse['Menu'].='</ul>';
			return;

			}
	if($_SESSION['usuario']=='user'){
			$this->_parse['Menu']='<ul id="menu">';
			$this->_parse['Menu'].=$this->Tparsetemplate($this->Tgettemplate('menu_jugador'), $lang);
			$this->_parse['Menu'].='</ul>';
			return;
			}
	$this->_parse['Menu']='';
	}
	/*****************************************************************************
	*	Generar pantalla index
	*
	******************************************************************************/
	function template_index()
	{
	global $lang,$lp_root_path,$phpEx;
	$this->TincludeLang('index');
	$this->_parse=$lang;
	$this->set_NombreBody('login_body');
	$this->set_NombreCabecera('head_general');
	$this->generaMenu();
	$this->mostrar();
	}
	function template_registro()
	{

	global $lang,$lp_root_path,$phpEx;
	$this->TincludeLang('registro');
	$this->_parse=$lang;
	$this->set_NombreBody('registro');
	$this->set_NombreCabecera('head_general');
	$this->generaMenu();
	$this->mostrar();

	}
	function pantallaDefecto()
	{

	global $lang,$lp_root_path,$phpEx;
			$this->_parse=$lang;
	
	$this->set_NombreBody('form_generico');
	$this->set_NombreCabecera('head_general');
	$this->_parse['Mensaje']='Welcome to Live Planet';	
	$this->generaMenu();
	$this->mostrar();

	}
	function pantallaGalaxia()
	{
	global $lang,$lp_root_path,$phpEx;
			$this->_parse=$lang;
	
	$this->set_NombreBody('form_generico');
	$this->set_NombreCabecera('head_general');
	$this->_parse['Mensaje']='Saldrá la pantalla de la galaxia';	
	$this->generaMenu();
	$this->mostrar();

	}
	function pantallaAlianza()
	{
	global $lang,$lp_root_path,$phpEx;
			$this->_parse=$lang;
	
	$this->set_NombreBody('form_generico');
	$this->set_NombreCabecera('head_general');
	$this->_parse['Mensaje']='Saldrá la pantalla alianza';	
	$this->generaMenu();
	$this->mostrar();

	}
	function pantallaSugerencias()
	{
		global $lang,$lp_root_path,$phpEx;
		$this->_parse=$lang;
		
		$this->set_NombreBody('form_generico');
		$this->set_NombreCabecera('head_general');
		$this->_parse['Mensaje']='Saldrá la pantalla sugerencias';	
		$this->generaMenu();
		$this->mostrar();
		
	}
	function pantallaFullAjax()
	{
		global $lang,$lp_root_path,$phpEx;
		$this->_parse=$lang;
		
		$this->set_NombreBody('form_ajax');
		$this->set_NombreCabecera('head_ajax');
		//$this->_parse['Mensaje']='Saldrá la pantalla sugerencias';	
		$this->generaMenu();
		$this->mostrar();
		
	}
	function pantallaAdministrar()
	{
	global $lang,$lp_root_path,$phpEx;
			$this->_parse=$lang;
	
	$this->set_NombreBody('form_generico');
	$this->set_NombreCabecera('head_general');
	$this->_parse['Mensaje']='Saldrá la pantalla de administrar';	
	$this->generaMenu();
	$this->mostrar();

	}
	function pantallaTesting()
	{
	global $lang,$lp_root_path,$phpEx;
			$this->_parse=$lang;
	
	$this->set_NombreBody('test_procedures');
	$this->set_NombreCabecera('head_general');
	$this->generaMenu();
	$this->mostrar();

	}
	
	function pantallaBuilidingInfo()
	{
	global $lang,$lp_root_path,$phpEx;
	$this->TincludeLang('info');
	$this->_parse=$lang;
	$this->set_NombreBody('building_info');
	$this->set_NombreCabecera('head_general');
	include_once ($lp_root_path ."classes/buildinginfo.class.".$phpEx);
	$infos = Buildinginfo::list_buildinginfo();
	$salida = '<div id="menuBox"><ul>';

	while($info= array_pop($infos))
	{	
		$cambios = array();
		$cambios = $lang;
		$cambios['infoBname'] = $info->get_Name();
		$cambios['infoBdescription'] = $info->get_description();
		$cambios['infoBpriceRatio'] = $info->get_priceRatio();
		$cambios['infoBpriceMetal'] = $info->get_priceMetal();
		$cambios['infoBpriceCrystal'] = $info->get_priceCrystal();
		$cambios['infoBpriceDeuterium'] = $info->get_priceDeuterium();
		$cambios['infoBpriceEnergy'] = $info->get_priceEnergy();
		$cambios['infoBstartLevel'] = $info->get_startLevel();
		$cambios['infoBorder'] = $info->get_order();
		$cambios['infoBmetalPerHour'] = $info->get_metalPerHour();
		$cambios['infoBcrystalPerHour'] = $info->get_crystalPerHour();
		$cambios['infoBdeuteriumPerHour'] = $info->get_deuteriumPerHour();
		$salida.='<li><a href="#">'.$info->get_Name().'</a><ul>'.$this->Tparsetemplate($this->Tgettemplate('building_info_step'), $cambios).'</ul></li>';
	}
	$salida .= '</ul></div>';
	$this->_parse['Info']=$salida;
	$this->generaMenu();
	$this->mostrar();

	}
function template_univers()
	{
	global $lang,$lp_root_path,$phpEx;
	$this->TincludeLang('univers');
	$this->_parse=$lang;
	$this->set_NombreBody('univers');
	$this->set_NombreCabecera('head_general');
	include_once ($lp_root_path ."classes/universes.class.".$phpEx);
	include_once ($lp_root_path ."classes/galaxies.class.".$phpEx);
	include_once ($lp_root_path ."classes/systems.class.".$phpEx);
	include_once ($lp_root_path ."classes/planets.class.".$phpEx);

	$universe = array_pop(Universes::list_universes());
	
	if ($universe!=null )
	{

		$this->_parse['uniName'] = $universe->get_Name();
		$galaxies=  Galaxies::list_galaxies('`universeId`="'.$universe->get_id().'"'); 	
		$this->_parse['uniNGalaxia'] = count ($galaxies);
		$num_systems=0;
		$num_planets=0;
		while($galaxie=array_pop($galaxies))
			{
				$systems=  Systems::list_systems('`galaxyId`="'.$galaxie->get_id().'"');
				
				$num_systems+=count($systems);
				while($system = array_pop($systems))
					{
					$planets = Planets::list_planets('`systemId`="'.$system->get_id().'"');
					$num_planets+=count($planets);					
					}			
			}
		$this->_parse['uniNSystems'] = $num_systems;
		$this->_parse['uniNPlanetCount'] = $num_planets;
		$this->_parse['uniNMaxUsers'] = $universe->get_MaxUsers();
	}
	else{echo'null';}	
	$this->_parse['Table']='<table width="100%" height="100%" border="1">';
	$this->_parse['Table'].='<TR>';

	for ($i=0;$i<15;$i++)
	{
		$this->_parse['Table'].='<TR><TH>'.$i.'<TD>1.9<TD>0.003<TD>40%';
	}

	$this->_parse['Table'].='</table>';
	
	$this->generaMenu();
	$this->mostrar();
	}
}
?>
